﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ArmoryData;
using ArmoryData.Character;
using System.Net;
using System.IO;

namespace ArmoryViewerMobile.Forms
{
    public partial class ProfileBase : UserControl
    {
        public CharacterInfo Character;

        public ProfileBase()
        {
            InitializeComponent();
        }

        public void LoadData()
        {
            String portraitDir;
            if (Character.Level >= 0 && Character.Level <= 59)
            {
                portraitDir = "wow-default";
            }
            else if (Character.Level >= 60 && Character.Level <= 69)
            {
                portraitDir = "wow";
            }
            else if (Character.Level >= 70 && Character.Level <= 79)
            {
                portraitDir = "wow-70";
            }
            else if (Character.Level >= 80)
            {
                portraitDir = "wow-80";
            }
            else
            {
                portraitDir = "wow-default";
            }
            string url = "http://www.wowarmory.com/_images/portraits/" + portraitDir + "/" + (int)Character.Gender + "-" + (int)Character.Race + "-" + (int)Character.Class + ".gif";
            picPortrait.Image = LoadImage(new Uri(url));

            lblName.Text = Character.Name;
            lblGuild.Text = Character.GuildName;
            lblInfo.Text = String.Format("Level {0} {1} {2} {3}", Character.Level, Character.Gender, Character.Race, Character.Class);
            lblRealm.Text = Character.Realm;
            lblBattlegroup.Text = Character.BattleGroup;
            TalentSpec spec = Character.TalentSpecs[0];
            lblSpec.Text = string.Format("{0} ({1}/{2}/{3})", spec.Name, spec.TreeOne, spec.TreeTwo, spec.TreeThree);
            picSpec.Image = LoadImage(new Uri("http://www.wowarmory.com/_images/icons/class/" + (int)Character.Class + "/talents/" + spec.Id +".gif"));
            
            lblProf1.Text = Character.Professions[0].Name;
            lblProf1Val.Text = string.Format("{0} / {1}", Character.Professions[0].Value, Character.Professions[0].Max);
            picProf1.Image = LoadImage(new Uri("http://www.wowarmory.com/_images/icons/professions/" + lblProf1.Text.ToLower() + "-sm.gif"));

            lblProf2.Text = Character.Professions[1].Name;
            lblProf2Val.Text = string.Format("{0} / {1}", Character.Professions[1].Value, Character.Professions[1].Max);
            picProf2.Image = LoadImage(new Uri("http://www.wowarmory.com/_images/icons/professions/" + lblProf2.Text.ToLower() + "-sm.gif"));

            lblHealthVal.Text = Character.CharacterBars.Health.Effective.ToString();
            lblPowerVal.Text = Character.CharacterBars.SecondBar.Effective.ToString();
            switch (Character.CharacterBars.SecondBar.Type)
            {
                case "r": lblPower.Text = "Rage"; break;
                case "m": lblPower.Text = "Mana"; break;
                case "e": lblPower.Text = "Energy"; break;
                case "p": lblPower.Text = "Runic"; break;
                default: lblPower.Text = "Mana"; break;
            }
        }

        private Image LoadImage(Uri uri)
        {
            WebResponse response = WebRequest.Create(uri).GetResponse();
            Stream img = response.GetResponseStream();
            Bitmap bmp = new Bitmap(img);
            response.Close();
            img.Close();
            return bmp;
        }
    }
}
